package com.study.volatile_study;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description 经典
 * @Author hgg
 * @Date 2021/12/14 11:37
 * @Version 1.0
 */
public class 经典 {

    //内存可见
    volatile boolean stop = false;

    static 经典 h = new 经典();

    private static final ExecutorService executor = new ThreadPoolExecutor(2,5,60, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(10));

    public static void main(String[] args) throws InterruptedException {

            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        doSomething();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        stopSomething();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

    }

    static void doSomething() throws InterruptedException {
        while(!h.stop){
            Thread.sleep(1000L);
            System.out.println(Thread.currentThread().getName() + " || 进来了");
        }
        System.out.println(Thread.currentThread().getName() + "do_main完成");
    }


    static void stopSomething() throws InterruptedException {
        //不同实例对象，相同名称的volatile变量，也不是同一份；
//        经典 h = new 经典();
        Thread.sleep(3000L);
        //线程2
        h.stop = true;

        System.out.println(Thread.currentThread().getName() + "stop_main完成");
    }
}
